package qianfeng.com.meijiadaka;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.lemon.view.RefreshRecyclerView;
import cn.lemon.view.adapter.Action;
import qianfeng.com.meijiadaka.adapter.gnk.GnkAdapter;
import qianfeng.com.meijiadaka.bean.gnk.GnkBean;
import qianfeng.com.meijiadaka.interfacebean.gnk.GnkData;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class JingxuanActivity extends AppCompatActivity {
    private String mUri = "http://home.naildaka.com/vapi/v3/home/knowledge/chosen?pageNo=1&categoryId=";

    @BindView(R.id.jixuan_rrv)
    RefreshRecyclerView mRecyclerView;
    private GnkAdapter mGnkAdapter;
    private int mId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jingxuan);
        ButterKnife.bind(this);
        mId= getIntent().getIntExtra("id", 1);
        init();
        loadData();
    }

    private void loadData() {
        new Retrofit.Builder().baseUrl("http://home.naildaka.com/").
                addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                build().create(GnkData.class).
                getData(mUri+mId).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Action1<GnkBean>() {
                    @Override
                    public void call(GnkBean gnkBean) {
                        mGnkAdapter.addAll(gnkBean.getResult());
                        mRecyclerView.dismissSwipeRefresh();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Toast.makeText(JingxuanActivity.this,"网络不好",Toast.LENGTH_SHORT).show();
                    }
                });
    }

    private void init() {
        mGnkAdapter = new GnkAdapter(this);
        mRecyclerView.setSwipeRefreshColors(0xFF437845, 0xFFE44F98, 0xFF2FAC21);
        mRecyclerView.setLayoutManager(new LinearLayoutManager((this)));
        mRecyclerView.setAdapter(mGnkAdapter);
        mRecyclerView.setRefreshAction(new Action() {
            @Override
            public void onAction() {
                mGnkAdapter.clear();
                loadData();
            }
        });
    }

    @OnClick({R.id.jingxuan_ib})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.jingxuan_ib:
                finish();
                break;
        }
    }
}
